package com.future.servlet;

import com.future.biz.DepartmenSurveytBIZ;
import com.future.model.bo.DepartmentSurveyBO;
import com.future.model.bo.UserBO;
import com.future.model.po.InforShareAnswerModel;
import com.future.model.po.OfficeModel;
import com.future.model.po.OfficeServicesModel;
import com.future.model.po.SecondFillBlankModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author petter   Jan 15, 2014
 * 对第二部分 部门调查（省市县三级政务部门）增删查改
 */
public class DepartmentServlet extends HttpServlet {
	private static Logger logger = LogManager.getLogger(DepartmentServlet.class);

	public DepartmentServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			String flag = request.getParameter("flag");
			if (flag != null && !("").equals(flag)) {
				int iflag = Integer.valueOf(flag);
				switch (iflag) {
				case 1:
					showDepartment(request, response);
					break;
				case 2:
					updateDepartment(request, response);
					break;
				}
			}

	}

	public void doPost(	HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	/**
	 * 显示第二部分 部门调查（省市县三级政务部门）页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void showDepartment(	HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/plain;charset=utf-8");
		request.setCharacterEncoding("utf-8");

		logger.debug("doPost");
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		DepartmenSurveytBIZ departmentBiz = new DepartmenSurveytBIZ();
		DepartmentSurveyBO departmentSurveyBo = departmentBiz.generateSureyByUserId(userBo.getUser().getId());

		request.setAttribute("departmentSurveyBo", departmentSurveyBo);
		request.getRequestDispatcher("/department_survey.jsp").forward(request, response);
		return;

	}
	/**
	 * 修改第二部分 部门调查（省市县三级政务部门）
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void updateDepartment(	HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		SecondFillBlankModel sBlankModel = getSFBM(request,response);
		List<OfficeModel> officeModels = this.getOfficeModel(request, response);
		List<InforShareAnswerModel> inforShareAnswerModels = this.getInforShareAnswerModel(request, response);
		List<OfficeServicesModel> officeServicesModels = this.getOfficeServicesModel(request, response);
		String rowspan = request.getParameter("rowspans");
		String[] rowspans = null;
		if (isNotbank(rowspan)) {
			rowspans = rowspan.split(",");
		}
		
		DepartmenSurveytBIZ departmentBiz = new DepartmenSurveytBIZ();
		
		boolean flag = departmentBiz.updateDepartment(userBo, sBlankModel,officeModels,
				inforShareAnswerModels,officeServicesModels,rowspans);
		response.setContentType("application/json; utf-8");
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		
		PrintWriter out = response.getWriter();
		out.println(flag);
		out.flush();
		out.close();
		return ;
	}
	
	private SecondFillBlankModel getSFBM(	HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		SecondFillBlankModel sBlankModel = new SecondFillBlankModel();
		sBlankModel.setItem0101(isNotbank(request.getParameter("item0101"))?Integer.valueOf(request.getParameter("item0101")):null);
		sBlankModel.setItem0102(isNotbank(request.getParameter("item0102"))?Integer.valueOf(request.getParameter("item0102")):null);
		sBlankModel.setItem0201(isNotbank(request.getParameter("item0201"))?Integer.valueOf(request.getParameter("item0201")):null);
		sBlankModel.setItem0202(isNotbank(request.getParameter("item0202"))?Float.valueOf(request.getParameter("item0202")):null);
		sBlankModel.setItem0203(isNotbank(request.getParameter("item0203"))?Integer.valueOf(request.getParameter("item0203")):null);
		sBlankModel.setItem0204(isNotbank(request.getParameter("item0204"))?Float.valueOf(request.getParameter("item0204")):null);
		sBlankModel.setItem0205(isNotbank(request.getParameter("item0205"))?Float.valueOf(request.getParameter("item0205")):null);
		sBlankModel.setItem0206(isNotbank(request.getParameter("item0206"))?Float.valueOf(request.getParameter("item0206")):null);
		sBlankModel.setItem0301(isNotbank(request.getParameter("item0301"))?Float.valueOf(request.getParameter("item0301")):null);
		sBlankModel.setItem0302(isNotbank(request.getParameter("item0302"))?Float.valueOf(request.getParameter("item0302")):null);
		sBlankModel.setItem0401(isNotbank(request.getParameter("item0401"))?Integer.valueOf(request.getParameter("item0401")):null);
		sBlankModel.setItem0402(isNotbank(request.getParameter("item0402"))?Integer.valueOf(request.getParameter("item0402")):null);
		sBlankModel.setUserid(userBo.getUser().getId());
		return sBlankModel;
	}
	
	private List<OfficeModel> getOfficeModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String officeids = request.getParameter("officeids");
		String officeNames = request.getParameter("officeNames");
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		List<OfficeModel> officeModels = new ArrayList<OfficeModel>();
		OfficeModel officeModel = null;
		String[] officeNamesArray = null;
		String[] officeidsArray = null; 
		if (isNotbank(officeNames)) {
			officeNamesArray = officeNames.split(",");
		}
		if (isNotbank(officeids)) {
			officeidsArray = officeids.split(",");
		}
		if (arrayIsNotBank(officeNamesArray)&&arrayIsNotBank(officeidsArray)) {
			for (int i = 0; i < officeNamesArray.length; i++) {
				officeModel = new OfficeModel();
				officeModel.setOffice(officeNamesArray[i]!=""?officeNamesArray[i]:null);
				officeModel.setUserid(userBo.getUser().getId());
				officeModels.add(officeModel);
			}
		} else if (arrayIsNotBank(officeNamesArray)) {
			for (int i = 0; i < officeNamesArray.length; i++) {
				officeModel = new OfficeModel();
				officeModel.setOffice(officeNamesArray[i]!=""?officeNamesArray[i]:null);
				officeModel.setUserid(userBo.getUser().getId());
				officeModels.add(officeModel);
			}
		}
		return officeModels;
	}
	
	private List<OfficeServicesModel> getOfficeServicesModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<OfficeServicesModel> officeServicesModels = new ArrayList<OfficeServicesModel>();
		OfficeServicesModel officeServicesModel = null;
		String servicess = request.getParameter("servicess");
		String issupports = request.getParameter("issupports");
		
        String[] servicessArray = null;
		String[] issupportsArray = null;
	    	servicessArray = servicess.split(",");
	    if(isNotbank(issupports))
	    	issupportsArray = issupports.split(",");
	    if (arrayIsNotBank(issupportsArray)) {
			for (int i = 0; i < servicessArray.length; i++) {
				officeServicesModel = new OfficeServicesModel();
				officeServicesModel.setIssupport(issupportsArray[i]!=""?Integer.valueOf(issupportsArray[i]):-1);
				officeServicesModel.setServices(servicessArray[i]!=""?servicessArray[i]:null);
				officeServicesModels.add(officeServicesModel);
			}
		} 
		return officeServicesModels;
	}
	
	
	private List<InforShareAnswerModel> getInforShareAnswerModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<InforShareAnswerModel> inforShareAnswerModels = new ArrayList<InforShareAnswerModel>();
		InforShareAnswerModel shareAnswerModel = null;
		String informationIds = request.getParameter("informationIds");
		String isShares = request.getParameter("isShares");
		String shareTypes = request.getParameter("shareTypes");
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		String[] informationIdsArray = null;
		String[] isSharesArray = null;
		String[] shareTypesArray = null;
		if (isNotbank(informationIds)) {
			informationIdsArray = informationIds.split(",");
		}
		if (isNotbank(isShares)) {
			isSharesArray = isShares.split(",");
		}
		if (isNotbank(shareTypes)) {
			shareTypesArray = shareTypes.split(";");
		}
		if (arrayIsNotBank(informationIdsArray)&&arrayIsNotBank(isSharesArray)&&
				arrayIsNotBank(shareTypesArray)) {
			for (int i = 0; i < informationIdsArray.length; i++) {
				shareAnswerModel = new InforShareAnswerModel();
				shareAnswerModel.setUserid(userBo.getUser().getId());
				shareAnswerModel.setInforid(informationIdsArray[i]!=""?Integer.valueOf(informationIdsArray[i]):null);
				shareAnswerModel.setIsShare(isSharesArray[i]!=""?Integer.valueOf(isSharesArray[i]):-1);
				shareAnswerModel.setShareType(shareTypesArray[i]!=""?shareTypesArray[i]:"-1");
				inforShareAnswerModels.add(shareAnswerModel);
			}
		}
		return inforShareAnswerModels;
	}
	
	private boolean isNotbank(String temp){
		if(temp!=null&&!("").equals(temp))
			return true;
		else 
			return false;
	}
	
	private boolean arrayIsNotBank(String[] strs) {
		
		if (strs!=null&&strs.length>0)
			return true;
		else
			return false;
	}
}
